﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuanLyChungCu_V.BussinessTỉer
{
    class StaffManagermentService
    {
        
        SQLConnection sql_conn;
        public StaffManagermentService()
        {
            try
            {
                sql_conn = new SQLConnection(null); // Lay ConnectionString trong DataConnection.xml, neu file khong ton tai thi tao moi va ghi lai ConnectionString
            }
            catch (ApplicationException ex)
            {
                throw ex;
            }
        }
      
        public void AddNhanVien(string name, int age)

        {
            QLCCDataContext ctx = new QLCCDataContext();
            ctx.Connection.ConnectionString = sql_conn.ConnectionString;
            ctx.NhanViens.InsertOnSubmit(new NhanVien
            {
                TenNV = name,
                TuoiNV = age,
                GioiTinh = true,
                DanToc = " ",
                idTonGiao = Guid.NewGuid(),

                idChucVu = Guid.NewGuid(),
                SoCMT = " ",
                DienThoai = " ",
            });
            ctx.SubmitChanges();
        }
        
      
       
        
        public IList<StaffDTO> FindStaff(string name)
        {
            QLCCDataContext ctx = new QLCCDataContext();
            ctx.Connection.ConnectionString = sql_conn.ConnectionString;

            var staff = from p in ctx.NhanViens
                         where p.TenNV.Contains(name)
                         select new StaffDTO
                         {
                             id = p.idNhanVien,
                             name = p.TenNV,
                             age = (int)p.TuoiNV,
                             id_card_no = p.SoCMT,
                             id_chucvu = p.idChucVu,
                             id_religion = p.idTonGiao,
                             phone_num = p.DienThoai,
                             race = p.DanToc,
                             religion = p.DanToc,
                             sex = p.GioiTinh
                         };
            return staff.ToList<StaffDTO>();
        }
        }
}


    
        
    


